package com.tuyenmonkey.mkloader.type;

import com.tuyenmonkey.mkloader.model.Circle;
import com.tuyenmonkey.mkloader.util.AnimatorValueUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

public class Sharingan extends LoaderView {

  private Circle eye;
  private Circle eyeBound;
  private Circle[] sharingans;
  private int numberOfSharingan;
  private float rotate;
  private float scale;
  private float eyeBoundRadius;
  private AnimatorValueUtil animatorValueUtil;

  public Sharingan() {
    numberOfSharingan = 3;
  }

  @Override public void initializeObjects() {

    float r = Math.min(width, height) / 2f;
    eyeBoundRadius = r / 1.5f;

    eye = new Circle();
    eye.setCenter(center.getPointX(), center.getPointY());
    eye.setColor(color);
    eye.setRadius(r / 4);

    eyeBound = new Circle();
    eyeBound.setCenter(center.getPointX(), center.getPointY());
    eyeBound.setColor(color);
    eyeBound.setRadius(eyeBoundRadius);
    eyeBound.setStyle(Paint.Style.STROKE_STYLE);
    eyeBound.setWidth(r / 20f);

    sharingans = new Circle[numberOfSharingan];
    for (int i = 0; i < numberOfSharingan; i++) {
      sharingans[i] = new Circle();
      sharingans[i].setCenter(center.getPointX(), center.getPointY() - eyeBoundRadius);
      sharingans[i].setColor(color);
      sharingans[i].setRadius(r / 6);
    }
  }

  @Override public void setUpAnimation() {
    AnimatorValue rotateAnimator = new AnimatorValue();
    rotateAnimator.setDuration(1500);
    rotateAnimator.setLoopedCount(AnimatorValue.INFINITE);
    rotateAnimator.setCurveType(Animator.CurveType.ACCELERATE);
    rotateAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
      @Override
      public void onUpdate(AnimatorValue animatorValue, float v) {
        rotate = v * 360;
        if (invalidateListener != null) {
          invalidateListener.reDraw();
        }
      }
    });
    rotateAnimator.start();
    AnimatorValue scaleAnimator = new AnimatorValue();
      scaleAnimator.setDuration(1000);
      scaleAnimator.setLoopedCount(AnimatorValue.INFINITE);
      scaleAnimator.setCurveType(Animator.CurveType.LINEAR);

    animatorValueUtil = new AnimatorValueUtil();
    animatorValueUtil.ofFloat(1,0.8f, 1);

      scaleAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
      @Override
      public void onUpdate(AnimatorValue animatorValue, float v) {
          scale = animatorValueUtil.getValue(v);
        if (invalidateListener != null) {
          invalidateListener.reDraw();
        }
      }
    });
    scaleAnimator.start();
  }

  @Override public void draw(Canvas canvas) {
    canvas.save();
    canvas.scale(scale, scale, center.getPointX(), center.getPointY());
    canvas.rotate(rotate, center.getPointX(), center.getPointY());
    eye.draw(canvas);
    eyeBound.draw(canvas);
    for (int i = 0; i < numberOfSharingan; i++) {
      canvas.save();
      canvas.rotate(i * 120, center.getPointX(), center.getPointY());
      sharingans[i].draw(canvas);
      canvas.restore();
    }
    canvas.restore();
  }
}
